/**
 * UserController handles user-related operations.
 *
 * @author Xiong Keqin
 * @since 2023-11-02
 */
package com.ourcraft.informationcollectionsystem.service.company.companyInfomation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ourcraft.informationcollectionsystem.entity.Company;
import com.ourcraft.informationcollectionsystem.mapper.CompanyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * CompanyInformationServiceImpl provide enterprise information related services interface implementation.
 *
 * @author Xiong Keqin
 * @since 2023-11-02
 */
@Service
public class CompanyInformationServiceImpl implements CompanyInformationService {
    @Autowired
    CompanyMapper companyMapper;

//    private String encryptPassword(String password){
//        return password;
//    }


    /**
     * Retrieves Company information by user ID.
     *
     * @param id The ID of the user.
     * @return Company information.
     */
    @Override
    public Company getCompanyInfo(Integer id) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        Company company = companyMapper.selectOne(queryWrapper);
        if (company != null) {
            // 返回company的所有信息给前端
            return company;
        }
        // 将所有的信息设置为 null，返回给前端
        return company;
    }

    /**
     * Retrieves Company information by company ID.
     *
     * @param id The ID of the company.
     * @return Company information.
     */
    @Override
    public Company getCompanyInfo2(Integer id) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id", id);
        Company company = companyMapper.selectOne(queryWrapper);
        if (company != null) {
            // 返回company的所有信息给前端
            return company;
        }
        // 将所有的信息设置为 null，返回给前端
        return company;
    }

    /**
     * Retrieves company ID by user ID.
     *
     * @param userId The ID of the user.
     * @return company ID.
     */
    @Override
    public Integer user2CompanyId(int userId){
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        Integer companyId = companyMapper.selectOne(queryWrapper).getCompanyId();
        return companyId;
    }

    /**
     * Update company information.
     *
     * @param comp1 The Company information.
     * @param is_Submit Whether the identification is submitted information.
     * @return Operation result.
     */
    @Override
    public Boolean submitCompanyInfo(Company comp1, boolean is_Submit) {
        Integer id = comp1.getUserId();
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("user_id", id);
        int status = companyMapper.selectOne(queryWrapper).getCompanyRecordStatus();

        // 设置状态字段
        if(is_Submit == true){
            comp1.setCompanyRecordStatus(2);
        }
        else if(status==0){
            comp1.setCompanyRecordStatus(1);
        }
        else{
            comp1.setCompanyRecordStatus(status);
        }

        // 使用MyBatis-Plus的update方法来更新数据
        int rowsUpdated = companyMapper.update(comp1, queryWrapper);

        if (rowsUpdated > 0) {
            return true;
        } else {
            return false;
        }


    }

}


